package com.dnndo.web.controller.system;


import com.dnndo.common.annotation.Log;
import com.dnndo.common.core.controller.BaseController;
import com.dnndo.common.core.domain.AjaxResult;
import com.dnndo.common.core.domain.R;
import com.dnndo.common.enums.BusinessType;
import com.dnndo.common.utils.bean.BeanDtoVoUtil;
import com.dnndo.system.domain.Menu;
import com.dnndo.system.domain.dto.MenuInsertDTO;
import com.dnndo.system.domain.dto.MenuUpdateDTO;
import com.dnndo.system.domain.vo.MenuVO;
import com.dnndo.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 菜单
 * 
 * @author dnndo
 * @date 2025-04-23
 */
@RestController
@RequestMapping("/system/naviMenu")
public class MenuController extends BaseController
{
    @Autowired
    private IMenuService menuService;
    /**
     * 获取树形结构
     */
    @GetMapping("/tree")
    @PreAuthorize("@ss.hasPermi('system:naviMenu:tree')")
    public AjaxResult tree()
    {
        return success(menuService.getTree());
    }

    /**
     * 获取菜单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:naviMenu:getInfo')")
    @GetMapping(value = "/{id}")
    public R<MenuVO> getInfo(@PathVariable("id") Long id)
    {
        Menu menu = menuService.selectMenuById(id);
        MenuVO menuVO = BeanDtoVoUtil.convert(menu, MenuVO.class);
        return R.ok(menuVO);
    }

    /**
     * 新增菜单
     */
    @PreAuthorize("@ss.hasPermi('system:naviMenu:add')")
    @Log(title = "菜单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated MenuInsertDTO menu)
    {
        return toAjax(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     */
    @PreAuthorize("@ss.hasPermi('system:naviMenu:edit')")
    @Log(title = "菜单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody @Validated MenuUpdateDTO menu)
    {
        return toAjax(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单(会删除此菜单的所有子级)
     */
    @PreAuthorize("@ss.hasPermi('system:naviMenu:del')")
    @Log(title = "菜单", businessType = BusinessType.DELETE)
	@GetMapping("/del/{id}")
    public AjaxResult del(@PathVariable(value = "id", name = "id") Long id)
    {
        return toAjax(menuService.deleteMenuById(id));
    }
}
